package com.data.modules.log.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.CrmResult;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.common.system.vo.DictModel;
import com.data.modules.system.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.data.common.api.vo.Result;
import com.data.modules.log.entity.ApiCheck;
import com.data.modules.log.entity.ApiLog;
import com.data.modules.log.service.IApiLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 请求三方api日志
 * @Author: data-boot
 * @Date:   2022-01-06
 * @Version: V1.0
 */
@Api(tags="请求三方api日志")
@RestController
@RequestMapping("/log/apiLog")
@Slf4j
public class ApiLogController extends BootController<ApiLog, IApiLogService> {
	@Autowired
	private IApiLogService apiLogService;

	/**
	 * 分页列表查询
	 *
	 * @param apiLog
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "请求三方api日志-分页列表查询")
	@ApiOperation(value="请求三方api日志-分页列表查询", notes="请求三方api日志-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ApiLog apiLog,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ApiLog> queryWrapper = QueryGenerator.initQueryWrapper(apiLog, req.getParameterMap());
		Page<ApiLog> page = new Page<ApiLog>(pageNo, pageSize);
		IPage<ApiLog> pageList = apiLogService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 调用阿里云api查询接口
	 * @param apiCheck
	 * @return
     * @auth xiayuanpeng
	 */
	@AutoLog(value = "调用阿里云api查询接口")
	@ApiOperation(value="调用阿里云api查询接口", notes="调用阿里云api查询接口")
	@PostMapping(value = "/apiCheck")
	public CrmResult<List<ApiLog>> apiCheck(@RequestBody ApiCheck apiCheck) {
		return CrmResult.OK(apiLogService.apiCheck(apiCheck.getTypes(), apiCheck.getRequestParam()));
	}
	/**
	 * 通过企业信用代码进行合作商认证
	 * @param socCode  企业信用代码
	 * @param company  企业名称
	 * @return
	 * @auth xiayuanpeng
	 */
	@AutoLog(value = "通过企业信用代码进行合作商认证")
	@ApiOperation(value="通过企业信用代码进行合作商认证", notes="通过企业信用代码进行合作商认证")
	@PostMapping(value = "/checkCompanyByCode")
	public CrmResult<List<ApiLog>> checkCompanyByCode(@RequestParam String socCode,@RequestParam String company) {
		log.debug("======企业信用代码==>{},=========企业名称======>{}",socCode,company);

		List<ApiLog> apiLogs = apiLogService.checkCompanyByCode(socCode, company);

		return CrmResult.OK(apiLogs);
	}
	/**
	 *   添加
	 *
	 * @param apiLog
	 * @return
	 */
	@AutoLog(value = "请求三方api日志-添加")
	@ApiOperation(value="请求三方api日志-添加", notes="请求三方api日志-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ApiLog apiLog) {
		apiLogService.save(apiLog);
		return Result.OK("添加成功！");
	}
	/**
	 *  编辑
	 *
	 * @param apiLog
	 * @return
	 */
	@AutoLog(value = "请求三方api日志-编辑")
	@ApiOperation(value="请求三方api日志-编辑", notes="请求三方api日志-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ApiLog apiLog) {
		apiLogService.updateById(apiLog);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "请求三方api日志-通过id删除")
	@ApiOperation(value="请求三方api日志-通过id删除", notes="请求三方api日志-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		apiLogService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "请求三方api日志-批量删除")
	@ApiOperation(value="请求三方api日志-批量删除", notes="请求三方api日志-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.apiLogService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "请求三方api日志-通过id查询")
	@ApiOperation(value="请求三方api日志-通过id查询", notes="请求三方api日志-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ApiLog apiLog = apiLogService.getById(id);
		if(apiLog==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(apiLog);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param apiLog
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ApiLog apiLog) {
        return super.exportXls(request, apiLog, ApiLog.class, "请求三方api日志");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ApiLog.class);
    }

	@AutoLog(value = "请求三方api日志-列表查询所有")
	@ApiOperation(value="请求三方api日志-列表查询所有", notes="请求三方api日志-分页列表查询")
	@PostMapping(value = "/queryAll")
	public Result<List<ApiLog>> queryAll(){
		List<ApiLog> apiLogs = apiLogService.queryAll();
		return Result.OK(apiLogs);
	}

}
